package com.atguigu.controller;

import com.atguigu.entity.Admin;
import com.atguigu.entity.Role;
import com.atguigu.service.AdminRoleService;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @title：
 * @author：Yu
 * @create：2023-02-04 9:56
 * @description：
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @DubboReference
    private AdminService adminService;
    @DubboReference
    private RoleService roleService;
    @DubboReference
    private AdminRoleService adminRoleService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping
    public String index(Map map, HttpServletRequest request) {
        Map<String, Object> filters = getFilters(request);
        map.put("filters", filters);
        PageInfo<Admin> page = adminService.findPage(filters);
        map.put("page", page);
        return "admin/index";
    }

    @RequestMapping("/save")
    public String create(Admin admin) {
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.insert(admin);
        return "common/success";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Map map) {
        Admin admin = adminService.findById(id);
        map.put("admin", admin);
        return "admin/edit";
    }

    @RequestMapping("/update")
    public String update(Admin admin) {
        adminService.update(admin);
        return "common/success";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        adminService.delete(id);
        return "common/success";
    }

    @RequestMapping("uploadShow/{adminId}")
    public String upload(@PathVariable Long adminId, Map map) {
        map.put("adminId", adminId);
        return "admin/upload";
    }

    @RequestMapping("/upload")
    public String uploads(Long adminId, @RequestParam("file") MultipartFile file) throws IOException {
        String fileName = UUID.randomUUID().toString();
        QiniuUtil.upload2Qiniu(file.getBytes(), fileName);
        String url = "http://rq6bjm5jq.hn-bkt.clouddn.com/" + fileName;
        adminService.upload(adminId, url);
        return "common/success";
    }

    @RequestMapping("/assignShow/{adminId}")
    public String assignShow(@PathVariable Long adminId, Map map) {
        //1.页面需要adminId,所以传过去
        map.put("adminId", adminId);
        //2.查询该用户所拥有和为拥有的数据信息，因为是两个list集合，因此用map返回
        Map<String, List<Role>> dateByAdminId = roleService.getDateByAdminId(adminId);
        //3.将返回的map集合放入形参的map集合中
        map.putAll(dateByAdminId);
        return "admin/assignShow";
    }

    @RequestMapping("/assignRole")
    public String assignRole(Long adminId, String[] roleIds) {
        //1.这里有两种接收roleIds字符串参数的方法，第一种是接收一个String类型的参数，通过字符串的spilt方法来转换成数组
        //第二种方法是spring自带的，可以直接将字符串转换为一个数组，但是该字符传必须是用逗号分割的
        adminRoleService.insertAdminRole(adminId, roleIds);
        return "common/success";
    }
}
